package com.genghis.rapidcrm.web.controller;

import com.genghis.rapidcrm.web.view.HomeIndexView;
import com.genghis.rapidcrm.web.view.support.ResponseData;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ThemeResolver;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.view.RedirectView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author xulixin
 */
@Controller
public class HomeController {

    private final ThemeResolver themeResolver;

    public HomeController(ThemeResolver themeResolver) {
        this.themeResolver = themeResolver;
    }

    @RequestMapping("/")
    public View index(HttpServletRequest request) {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        if ("admin".equals(username) && password != null) {
            return new HomeIndexView();
        }
        return new RedirectView("login");
    }

    @RequestMapping("/skin/change")
    @ResponseBody
    public ResponseData changeTheme(@RequestParam String value, HttpServletRequest request, HttpServletResponse response) {
        themeResolver.setThemeName(request, response, value);
        return new ResponseData();
    }
}
